package com.yonyougov.devops.cruxcisupport.subscribe;

import com.yonyougov.devops.cruxcisupport.component.UserStore;
import com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeCancelDto;
import com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeDto;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * t_subscribe服务实现
 */
@Service
@Slf4j
public class SubscribeServiceImpl extends BaseServiceImpl<SubscribeEntity, BigInteger> implements SubscribeService {
    private final SubscribeDao subscribeDao;
    @Autowired
    private UserStore userStore;

    public SubscribeServiceImpl(SubscribeDao subscribeDao) {
        this.subscribeDao = subscribeDao;
    }

    @Override
    public BaseDao<SubscribeEntity, BigInteger> getDao() {
        return subscribeDao;
    }

    //<!--用户自定义代码开始-->
    /**
     * 当前用户订阅应用
     *
     * @param subscribeDto [com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeDto]
     */
    @Override
    public void subscribe(SubscribeDto subscribeDto) {
        List<SubscribeEntity> subscribeEntityList = subscribeDto.getAppIds().stream().map(appId -> {
            SubscribeEntity subscribeEntity = new SubscribeEntity();
            subscribeEntity.setId(new CruxDevOpsIdGenerator().generatorId(subscribeDao));
            subscribeEntity.setUserId(userStore.getCurrentUser().getId());
            subscribeEntity.setAppId(appId);
            subscribeEntity.setCreateTime(new Date());
            return subscribeEntity;
        }).collect(Collectors.toList());
        subscribeDao.batchInsert(subscribeEntityList);
    }

    /**
     * 当前用户取消订阅应用
     *
     * @param subscribeCancelDto [com.yonyougov.devops.cruxcisupport.subscribe.dto.SubscribeCancelDto]
     */
    @Override
    public void cancelSubscribe(SubscribeCancelDto subscribeCancelDto) {
        List<SubscribeEntity> subscribeEntityList = subscribeCancelDto.getAppIds().stream().map(appId -> {
            SubscribeEntity subscribeEntity = new SubscribeEntity();
            subscribeEntity.setAppId(appId);
            return subscribeEntity;
        }).collect(Collectors.toList());
        subscribeDao.batchDelete(subscribeEntityList);
    }

    /**
     * 当前用户已订阅的应用列表
     *
     * @return java.util.List<com.yonyougov.devops.cruxcisupport.subscribe.SubscribeEntity>
     */
    @Override
    public List<SubscribeEntity> findAppsOfCurrentUserSub() {
        SubscribeEntity subscribeEntity = new SubscribeEntity();
        subscribeEntity.setPageAble(false);
        subscribeEntity.setUserId(userStore.getCurrentUser().getId());
        List<SubscribeEntity> datas = subscribeDao.findAllBy(subscribeEntity);
        return datas;
    }
    //<!--用户自定义代码结束-->
}
